<template>
  <div class="login-container">
    <!-- 显示登录控件的区域 -->
    <div class="login-box">
      <!-- 项目logo -->
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="vue_shop" />
      </div>
      <!-- 用户登录表单 -->
      <el-form label-width="80px" class="login-form" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <!-- 登录和重置按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  // 组件用到的数据
  data() {
    return {
      // 登录表单
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      // 表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },

  // 组件用到的方法
  methods: {
    // 点击重置按钮,表单中的字段重置为默认值
    resetLoginForm() {
      // 获取表单元素
      // console.dir(this.$refs.loginForm)
      // 表单中的字段重置为默认值
      this.$refs.loginFormRef.resetFields()
    },

    // 点击登录按钮,进行登录
    login() {
      // 进行表单验证
      this.$refs.loginFormRef.validate(async (valid) => {
        // 表单验证失败，退出
        if (!valid) return
        // 发送post请求登录
        const { data: res } = await this.$http.post('login', this.loginForm)
        // 登录失败，提示信息
        if (res.meta.status != 200)
          return this.$message({
            type: 'error',
            message: '登录失败',
            duration: 1500,
          })
        // 登录成功
        this.$message.success('登录成功')
        // 保存token到localStage
        window.sessionStorage.setItem('token', res.data.token)
        // 跳转主页
        this.$router.push('/home')
      })
    },
  },
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background: rgb(43, 75, 107);
}

.login-box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 5px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar-box {
  width: 130px;
  height: 130px;
  border: 1px #eee solid;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  background-color: #fff;
  padding: 10px;
  position: relative;
  left: 50%;
  transform: translate(-50%, -50%);
  img {
    width: 100%;
    height: 100%;
    background-color: #eee;
    border-radius: 50%;
  }
}
.login-form {
  position: absolute;
  box-sizing: border-box;
  width: 100%;
  padding: 0 20px;
  bottom: 0;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
